package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.vo.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Async
public class ArticleFreemarkerServiceImpl implements ArticleFreemarkerService {
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private Configuration freemarkerConfig;
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    @Override
    public void buildArticleToMinio(ApArticle apArticle, String content) {
        try {
            if (StringUtils.isNotBlank(content)) {
                //文章内容通过freemarker生成html文件
                Template template = freemarkerConfig.getTemplate("article.ftl");

                // 转换时间戳为Date对象
                Date publishDate = new Date(apArticle.getPublishTime().getTime());
                //数据模型
                Map<String, Object> map = new HashMap<>();
                map.put("content", JSONArray.parseArray(content));
                map.put("title", apArticle.getTitle());
                map.put("publishDate", publishDate);
                StringWriter out = new StringWriter();
                //合成
                template.process(map, out);
                //把html文件上传到minio中
                InputStream in = new ByteArrayInputStream(out.toString().getBytes());
                String path = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", in);
                //修改ap_article表，保存static_url字段
                LambdaUpdateWrapper<ApArticle> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(ApArticle::getId, apArticle.getId()).set(ApArticle::getStaticUrl, path);
                apArticleService.update(wrapper);


                //静态页面创建好后，发送消息，通知es添加索引库中的文章信息
                SearchArticleVo searchArticleVo = new SearchArticleVo();
                BeanUtils.copyProperties(apArticle, searchArticleVo);
                searchArticleVo.setContent(content);
                searchArticleVo.setStaticUrl(path);
                kafkaTemplate.send("insert-article-topic", JSONArray.toJSONString(searchArticleVo));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
